package cn.itcast.com.JDBC.JDBCUtils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.*;
import java.util.Properties;

/**
 * Created by 郭浩 on 2020/9/20 19:12
 */
public class JDBCUtils {
    private static Properties properties;

    static {
        FileInputStream inputStream = null;
        try {
            inputStream = new FileInputStream(new File("D:\\代码\\代码\\代码\\cn\\itcast\\com\\JDBC\\jdbc.properties"));
            properties = new Properties();
            properties.load(inputStream);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    public static Connection getConnection() throws ClassNotFoundException, SQLException {
        String user = properties.getProperty("user");
        String password = properties.getProperty("password");
        String driver = properties.getProperty("driverClassName");
        String url = properties.getProperty("url");
        Class.forName(driver);
        return DriverManager.getConnection(url, user, password);
    }


    public static void releaseResources(Connection connection, ResultSet resultSet, Statement statement) throws SQLException {
        if(connection != null) {
            connection.close();
        }
        if(statement != null){
            statement.close();
        }
        if(resultSet != null){
            resultSet.close();
        }
    }
}
